package com.matriq.cloudmanager.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.ConsoleActivity;
import com.matriq.cloudmanager.MyMessaggeActivity;
import com.matriq.cloudmanager.MycourseActivity;
import com.matriq.cloudmanager.NoticeAnnoActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import net.lgpage.fragment.BaseFragment;
import net.lgpage.tool.ImageTool;
import net.lgpage.view.ViewpagerIndicator;

public class MainFragment extends BaseFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fm_main, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setViews();
		setEvents();
		loadData();
	}

	LinearLayout ll[] = new LinearLayout[4];
	TextView tv[] = new TextView[4];
	TextView tv_title;
	ScrollView sv;
	android.support.v4.view.ViewPager vp;

	ViewpagerIndicator vpi;
	private void setViews() {
		vpi = (ViewpagerIndicator) getView().findViewById(R.id.vpi);
		ll[0] = (LinearLayout) getView().findViewById(R.id.ll0);
		ll[1] = (LinearLayout) getView().findViewById(R.id.ll1);
		ll[2] = (LinearLayout) getView().findViewById(R.id.ll2);
		ll[3] = (LinearLayout) getView().findViewById(R.id.ll3);
		tv[0] = (TextView) getView().findViewById(R.id.tv0);
		tv[1] = (TextView) getView().findViewById(R.id.tv1);
		tv[2] = (TextView) getView().findViewById(R.id.tv2);
		tv[3] = (TextView) getView().findViewById(R.id.tv3);
		tv_title = (TextView) getView().findViewById(R.id.tv_title);
		sv = (ScrollView) getView().findViewById(R.id.sv);
		vp = (android.support.v4.view.ViewPager) getView().findViewById(R.id.vp);
		pa = new ACPagerAdapter();
		vp.setAdapter(pa);
		vpi.setViewPager(vp);
	}

	private void setEvents() {
		ll[0].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toActivity(NoticeAnnoActivity.class);
			}
		});
		ll[1].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toActivity(MycourseActivity.class);
			}
		});
		ll[2].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toActivity(MyMessaggeActivity.class);
			}
		});
	}

	PagerAdapter pa;
	List<View> vlist;
	List<Object> vplist;

	private class ACPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			if (vlist == null) {
				return 0;
			}
			return vlist.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View v = vlist.get(position);
			container.addView(v);
			return v;
		}
	}

	public void loadVp(List<String> rlist) {
		if (rlist == null || rlist.size() == 0) {

		} else {
			if (vlist == null) {
				vlist = new ArrayList<View>();
			}
			for (int i = vlist.size(); i < rlist.size(); i++) {
				View v;
				ViewHolder h;
				if (i >= vlist.size()) {
					v = new ImageView(getActivity());
					// v = this.getLayoutInflater().inflate(R.layout.index_item,
					// null);
					h = new ViewHolder();
					h.iv = (ImageView) v;
					h.iv.setScaleType(ScaleType.FIT_XY);
					ImageTool.loadImage(rlist.get(i), h.iv);
					v.setTag(h);
					vlist.add(v);
					h.iv.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							toActivity(ConsoleActivity.class);
						}
					});
				} else {

				}
			}
			startLoop();
		}
		pa.notifyDataSetChanged();
	}

	Timer timer;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				int cur = vp.getCurrentItem();
				if (cur >= vlist.size() - 1) {
					cur = 0;
				} else {
					cur++;
				}
				vp.setCurrentItem(cur);
			}
			super.handleMessage(msg);
		}

	};

	private void startLoop() {
		if (timer != null) {
			timer.cancel();
		}
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				handler.sendEmptyMessage(1);
			}
		}, 3000, 3000);
	}

	class ViewHolder {
		ImageView iv;
	}

	Object obj;

	public void setData(Object obj) {
		this.obj = obj;
		tv[0].setText(null);
		tv[1].setText(null);
		tv[2].setText(null);
		tv[3].setText(null);
		tv_title.setText(null);
	}

	private void loadData() {
		List<String> rlist = new ArrayList<>();
		rlist.add("http://203.195.219.118:8080/tomcat.png");
		rlist.add("http://203.195.219.118:8080/tomcat.png");
		loadVp(rlist);
		vpi.setViewPager(vp);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}